package com.sxkf.template.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxkf.core.api.vo.R;
import com.sxkf.template.entity.AfterSaleOrder;
import com.sxkf.template.service.AfterSaleOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.util.CheckEmptyUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 售后订单表 前端控制器
 * </p>
 *
 * @author 杨玉龙
 * @since 2024-12-04 16:21:16
 */
@Api(tags = "售后模块")
@RestController
@RequestMapping("/afterSaleOrder")
public class AfterSaleOrderController {
    @Resource
    private AfterSaleOrderService afterSaleOrderService;


    @ApiOperation("查询所有售后订单")
    @GetMapping(value = "/page")
    public R<IPage<AfterSaleOrder>> page(@RequestParam Integer pageNo,@RequestParam Integer pageSize,
                                         @RequestParam(required = false) String state,
                                         @RequestParam(required = false) String orderNo){
        Page<AfterSaleOrder> page = afterSaleOrderService.page(new Page<>(pageNo, pageSize), new LambdaQueryWrapper<AfterSaleOrder>()
                .eq(CheckEmptyUtil.isNotEmpty(state), AfterSaleOrder::getState, state)
                .like(CheckEmptyUtil.isNotEmpty(orderNo), AfterSaleOrder::getOrderNo, orderNo)
        );

        page.getRecords().forEach(item -> {
            item.setExpectTime(DateUtil.offsetDay(item.getCreateTime(),item.getExpectDays()));
        });

        return R.success(page);
    }

    @ApiOperation("查一条订单")
    @GetMapping("/one")
    public R<AfterSaleOrder> getOne(@RequestParam Long id){
        AfterSaleOrder afterSaleOrder = afterSaleOrderService.getById(id);
        afterSaleOrder.setExpectTime(DateUtil.offsetDay(afterSaleOrder.getCreateTime(),afterSaleOrder.getExpectDays()));
        return R.success(afterSaleOrder);
    }

    @ApiOperation(value = "申请售后")
    @PostMapping(value = "/apply")
    public R<?> apply(@RequestBody AfterSaleOrder afterSaleOrder){
        afterSaleOrderService.apply(afterSaleOrder);
        return R.success();
    }

    @ApiOperation(value = "通过审核")
    @PostMapping(value = "/pass")
    public R<?> pass(@RequestParam String id){
        afterSaleOrderService.pass(id);
        return R.success();
    }

    @ApiOperation(value = "驳回审核")
    @PostMapping(value = "/reject")
    public R<?> reject(@RequestParam String id,@RequestParam String reason){
        afterSaleOrderService.reject(id,reason);
        return R.success();
    }
}
